package com.mpgame.database.mysql.service.game;

import com.mpgame.database.mysql.mapper.game.MonsterHotbarMapper;
import com.mpgame.database.mysql.model.game.MonsterHotbar;
import com.mpgame.database.mysql.model.game.MonsterStore;
import com.mpgame.database.mysql.model.game.custom.MonsterDisplayInfo;
import com.mpgame.database.mysql.utils.MyBatisUtil;
import com.mpgame.utils.Log;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MonsterHotbarService {
    
    public int createMonsterHotbar(MonsterHotbar monsterHotbar) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterHotbarMapper mapper = sqlSession.getMapper(MonsterHotbarMapper.class);
            int res = mapper.insert(monsterHotbar);
            sqlSession.commit();
            
            if (res > 0) {
                Log.info("创建怪物热槽位成功: " + monsterHotbar.getId());
                return monsterHotbar.getId();
            } else {
                Log.err("创建怪物热槽位失败");
                return 0;
            }
        } catch (Exception e) {
            Log.err("创建怪物热槽位异常: " + e.getMessage());
            throw new RuntimeException("创建怪物热槽位异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public MonsterHotbar getMonsterHotbarById(int id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterHotbarMapper mapper = sqlSession.getMapper(MonsterHotbarMapper.class);
            return mapper.selectById(id);
        } catch (Exception e) {
            Log.err("获取怪物热槽位失败: " + e.getMessage());
            throw new RuntimeException("获取怪物热槽位异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public MonsterHotbar getMonsterHotbarByPlayerId(int playerId) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterHotbarMapper mapper = sqlSession.getMapper(MonsterHotbarMapper.class);
            return mapper.selectByPlayerId(playerId);
        } catch (Exception e) {
            Log.err("根据玩家ID获取怪物热槽位失败: " + e.getMessage());
            throw new RuntimeException("根据玩家ID获取怪物热槽位异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    /**
     * 根据玩家ID直接获取热槽位中的MonsterStore数组
     * @param playerId 玩家ID
     * @return MonsterStore数组，如果玩家没有热槽位配置或槽位为空则返回空数组
     */
    public List<MonsterStore> getMonsterStoresByPlayerId(int playerId) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterHotbarMapper mapper = sqlSession.getMapper(MonsterHotbarMapper.class);
            
            List<MonsterStore> monsterStores = mapper.selectMonsterStoresByPlayerId(playerId);
            
            Log.info("玩家 " + playerId + " 热槽位中共有 " + monsterStores.size() + " 个怪物");
            return monsterStores;
            
        } catch (Exception e) {
            Log.err("根据玩家ID获取热槽位怪物失败: " + e.getMessage());
            throw new RuntimeException("根据玩家ID获取热槽位怪物异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    /**
     * 根据玩家ID和槽位索引获取指定的MonsterStore
     * @param playerId 玩家ID
     * @param slotIndex 槽位索引 (0-5)
     * @return MonsterStore，如果槽位为空则返回null
     */
    public MonsterStore getMonsterStoreByPlayerIdAndSlot(int playerId, int slotIndex) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterHotbarMapper mapper = sqlSession.getMapper(MonsterHotbarMapper.class);
            
            MonsterStore store = mapper.selectMonsterStoreByPlayerIdAndSlot(playerId, slotIndex);
            
            if (store != null) {
                Log.info("获取玩家 " + playerId + " 槽位 " + slotIndex + " 的怪物: " + store.getId());
            } else {
                Log.info("玩家 " + playerId + " 的槽位 " + slotIndex + " 为空");
            }
            
            return store;
            
        } catch (Exception e) {
            Log.err("根据玩家ID和槽位获取怪物失败: " + e.getMessage());
            throw new RuntimeException("根据玩家ID和槽位获取怪物异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public List<MonsterHotbar> getAllMonsterHotbars() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterHotbarMapper mapper = sqlSession.getMapper(MonsterHotbarMapper.class);
            return mapper.selectAll();
        } catch (Exception e) {
            Log.err("获取所有怪物热槽位失败: " + e.getMessage());
            throw new RuntimeException("获取所有怪物热槽位异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public boolean updateMonsterHotbar(MonsterHotbar monsterHotbar) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterHotbarMapper mapper = sqlSession.getMapper(MonsterHotbarMapper.class);
            int res = mapper.updateById(monsterHotbar);
            sqlSession.commit();
            return res > 0;
        } catch (Exception e) {
            Log.err("更新怪物热槽位失败: " + e.getMessage());
            throw new RuntimeException("更新怪物热槽位异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    /**
     * 根据玩家ID获取热槽位中的完整怪物展示信息
     * 使用单个SQL查询，性能最优，包含怪物和技能的所有信息
     * @param playerId 玩家ID
     * @return MonsterDisplayInfo数组
     */
    public List<MonsterDisplayInfo> getMonsterDisplayInfosByPlayerId(int playerId) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterHotbarMapper mapper = sqlSession.getMapper(MonsterHotbarMapper.class);
            
            List<MonsterDisplayInfo> displayInfos = mapper.selectMonsterDisplayInfosByPlayerId(playerId);
            
            Log.info("玩家 " + playerId + " 热槽位中共有 " + displayInfos.size() + " 个怪物展示信息");
            return displayInfos;
            
        } catch (Exception e) {
            Log.err("根据玩家ID获取怪物展示信息失败: " + e.getMessage());
            throw new RuntimeException("根据玩家ID获取怪物展示信息异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    /**
     * 根据玩家ID和槽位索引获取指定的完整怪物展示信息
     * @param playerId 玩家ID
     * @param slotIndex 槽位索引 (0-5)
     * @return MonsterDisplayInfo，如果槽位为空则返回null
     */
    public MonsterDisplayInfo getMonsterDisplayInfoByPlayerIdAndSlot(int playerId, int slotIndex) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterHotbarMapper mapper = sqlSession.getMapper(MonsterHotbarMapper.class);
            
            MonsterDisplayInfo displayInfo = mapper.selectMonsterDisplayInfoByPlayerIdAndSlot(playerId, slotIndex);
            
            if (displayInfo != null) {
                Log.info("获取玩家 " + playerId + " 槽位 " + slotIndex + " 的怪物展示信息: " + displayInfo.getMonsterName());
            } else {
                Log.info("玩家 " + playerId + " 的槽位 " + slotIndex + " 为空");
            }
            
            return displayInfo;
            
        } catch (Exception e) {
            Log.err("根据玩家ID和槽位获取怪物展示信息失败: " + e.getMessage());
            throw new RuntimeException("根据玩家ID和槽位获取怪物展示信息异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}